
import Head from 'next/head'

<Head>
  <script>
    {
      `(function() {
         var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?e60fb290e204e04c5cb6f79b0ac1e697";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
       })();`
    }
  </script>
</Head>

![LangChain](https://pica.zhimg.com/50/v2-56e8bbb52aa271012541c1fe1ceb11a2_r.gif)





抱抱脸（Huggingface）本地管道[#](#hugging-face-local-pipelines "链接到此标题的永久链接")
========================================================

Hugging Face 模型可以通过 `HuggingFacePipeline` 类在本地运行。

[Hugging Face 模型中心](https://huggingface.co/models) 托管超过 120k 个模型、20k 个数据集和 50k 个演示应用程序（Spaces)，全部都是开源且公开可用的，是一个在线平台，人们可以轻松协作和构建机器学习。

这些模型可以通过本地管道包装器或通过 HuggingFaceHub 类调用其托管的推断端点从 LangChain 中调用。有关托管管道的更多信息，请参见 [HuggingFaceHub](huggingface_hub.html) 教程。

要使用，您应该安装 `transformers` python [包。](https://pypi.org/project/transformers/)

```python
!pip install transformers > /dev/null

```

加载模型[#](#load-the-model "链接到此标题的永久链接")
--------------------------------------

```python
from langchain import HuggingFacePipeline

llm = HuggingFacePipeline.from_model_id(model_id="bigscience/bloom-1b7", task="text-generation", model_kwargs={"temperature":0, "max_length":64})

```

```python
WARNING:root:Failed to default session, using empty session: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /sessions (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x1117f9790>: Failed to establish a new connection: [Errno 61] Connection refused'))

```

将模型集成到LLMChain中[#](#integrate-the-model-in-an-llmchain "此标题的永久链接")
------------------------------------------------------------------

```python
from langchain import PromptTemplate,  LLMChain

template = """Question: {question}

Answer: Let's think step by step."""
prompt = PromptTemplate(template=template, input_variables=["question"])

llm_chain = LLMChain(prompt=prompt, llm=llm)

question = "What is electroencephalography?"

print(llm_chain.run(question))

```

```python
/Users/wfh/code/lc/lckg/.venv/lib/python3.11/site-packages/transformers/generation/utils.py:1288: UserWarning: Using `max_length`'s default (64) to control the generation length. This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation.
  warnings.warn(
WARNING:root:Failed to persist run: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /chain-runs (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x144d06910>: Failed to establish a new connection: [Errno 61] Connection refused'))

```

```python
 First, we need to understand what is an electroencephalogram. An electroencephalogram is a recording of brain activity. It is a recording of brain activity that is made by placing electrodes on the scalp. The electrodes are placed

```

